import { Vector2D } from '../utils/Vector2D';

type InputState = {
    direction: Vector2D;
    boost: boolean;
    shield: boolean;
};

export class InputManager {
    private currentInput: InputState;
    private keys: Set<string>;
    private touchStart: Vector2D | null;
    private touchPosition: Vector2D | null;
    private readonly joystickRadius: number = 50;

    constructor() {
        this.currentInput = {
            direction: new Vector2D(0, 0),
            boost: false,
            shield: false
        };
        this.keys = new Set();
        this.touchStart = null;
        this.touchPosition = null;

        this.initializeKeyboardControls();
        this.initializeTouchControls();
    }

    private initializeKeyboardControls(): void {
        window.addEventListener('keydown', (e: KeyboardEvent) => {
            this.keys.add(e.key.toLowerCase());
            this.updateKeyboardInput();
        });

        window.addEventListener('keyup', (e: KeyboardEvent) => {
            this.keys.delete(e.key.toLowerCase());
            this.updateKeyboardInput();
        });
    }

    private initializeTouchControls(): void {
        window.addEventListener('touchstart', (e: TouchEvent) => {
            const touch = e.touches[0];
            this.touchStart = new Vector2D(touch.clientX, touch.clientY);
            this.touchPosition = this.touchStart.clone();
        });

        window.addEventListener('touchmove', (e: TouchEvent) => {
            if (this.touchStart) {
                const touch = e.touches[0];
                this.touchPosition = new Vector2D(touch.clientX, touch.clientY);
                this.updateTouchInput();
            }
        });

        window.addEventListener('touchend', () => {
            this.touchStart = null;
            this.touchPosition = null;
            this.currentInput.direction = new Vector2D(0, 0);
        });
    }

    private updateKeyboardInput(): void {
        let dx = 0;
        let dy = 0;

        // 8方向键盘控制
        if (this.keys.has('w') || this.keys.has('arrowup')) dy -= 1;
        if (this.keys.has('s') || this.keys.has('arrowdown')) dy += 1;
        if (this.keys.has('a') || this.keys.has('arrowleft')) dx -= 1;
        if (this.keys.has('d') || this.keys.has('arrowright')) dx += 1;

        // 特殊按键
        this.currentInput.boost = this.keys.has('shift');
        this.currentInput.shield = this.keys.has('space');

        // 标准化方向向量
        if (dx !== 0 || dy !== 0) {
            this.currentInput.direction = new Vector2D(dx, dy).normalize();
        } else {
            this.currentInput.direction = new Vector2D(0, 0);
        }
    }

    private updateTouchInput(): void {
        if (this.touchStart && this.touchPosition) {
            // 计算触摸方向向量
            const delta = this.touchPosition.subtract(this.touchStart);
            
            // 如果超出虚拟摇杆半径，进行标准化
            if (delta.magnitude() > this.joystickRadius) {
                this.currentInput.direction = delta.normalize();
            } else {
                this.currentInput.direction = delta.divide(this.joystickRadius);
            }

            // 检测双指触摸作为加速
            this.currentInput.boost = document.querySelectorAll('touchstart').length > 1;
        }
    }

    public getInput(): InputState {
        return {
            direction: this.currentInput.direction.clone(),
            boost: this.currentInput.boost,
            shield: this.currentInput.shield
        };
    }
}